layui.use(['table', 'form', 'jquery', 'laydate', 'layer'], function () {
    let table = layui.table;
    let form = layui.form;
    let laydate = layui.laydate;
    let layer = layui.layer;
    let $ = layui.jquery;

    let BIZ_NAME = 'item';
    let BIZ_TABLE = 'item-table';
    let BIZ_FORM = 'item-form';
    let BIZ_QUERY = 'item-query';
    let tableItemObj;
    let formItemObj;

    let cols = [
        [
            {type: 'checkbox'},
            {title: '项目ID', field: 'id', align: 'center'},
            {title: '项目名称', field: 'name', align: 'center'},
            {title: '项目代码', field: 'code', align: 'center'},
            {title: '含税价格', field: 'price', align: 'center'},
            {title: '未税价格', field: 'untaxedPrice', align: 'center'},
            {title: '税率', field: 'taxRate', align: 'center'},
            {title: '服务内容', field: 'serviceContent', align: 'center'},
            {title: '创建时间', field: 'time', align: 'center'},
            {title: '操作', toolbar: '#item-operate-bar', align: 'center', width: 130}
        ]
    ]


    tableItemObj = table.render({
        elem: '#' + BIZ_TABLE,
        url: '/' + BIZ_NAME + '/page',
        where: {},
        //data: [{id:1}], // 测试数据
        text: {none: '暂无相关数据'},
        response: {
            statusCode: 200,
            msgName: 'message'
        },
        page: true,
        limit: 10,
        limits: [10, 20, 50],
        cols: cols,
        skin: 'line',
        toolbar: '#item-toolbar',
        defaultToolbar: [{
            layEvent: 'refresh',
            icon: 'layui-icon-refresh',
        }, 'filter', 'print', 'exports']
    });

    table.on('tool(' + BIZ_TABLE + ')', function (obj) {
        if (obj.event === 'remove') {
            window.removeItem(obj);
        } else if (obj.event === 'edit') {
            window.itemForm(obj);
        }
    });

    table.on('toolbar(' + BIZ_TABLE + ')', function (obj) {
        if (obj.event === 'add') {
            window.itemForm();
        } else if (obj.event === 'refresh') {
            window.refreshItem();
        }
    });


    window.itemForm = function (obj) {
        let title;
        if (obj) {
            title = '修改';
        } else {
            title = '新增';
        }
        formItemObj = layer.open({
            type: 1,
            title: title,
            shade: 0.1,
            area: ['70%', '85%'],
            btn: ['立即提交', '取消'],
            yes: function (index, layero) {
                $('#' + BIZ_FORM + ' [lay-submit]').click();
            },
            cancel: function (index, layero) {
                layer.close(index);
            },
            content: $('#' + BIZ_FORM),
            success: function (layero, index) {
                if (obj) {
                    // 修改时填充数据
                    form.val(BIZ_FORM, obj.data);
                } else {
                    // 新增时清空表单
                    form.val(BIZ_FORM, {});
                    $('#itemFormId').val('');
                    $('#itemFormReset').click();
                }
            }
        });
    }

    window.removeItem = function (obj) {
        layer.confirm('确定要删除', {icon: 3, title: '提示'}, function (index) {
            layer.close(index);
            let loading = layer.load(2, {time: 5 * 1000});
            $.ajax({
                url: "/" + BIZ_NAME + "/remove/" + obj.data['id'],
                type: 'DELETE',
                success: function (result) {
                    layer.close(loading);
                    if (result.code === 200) {
                        layer.msg(result.message, {icon: 1, time: 1000}, function () {
                            obj.del();
                        });
                    } else {
                        layer.alert(result.message, {icon: 2});
                    }
                }
            })
        });
    }

    window.refreshItem = function (param) {
        tableItemObj.reload();
    }

    laydate.render({
        elem: '#itemStart'
        , type: 'datetime'
    });

    laydate.render({
        elem: '#itemEnd'
        , type: 'datetime'
    });

    form.on('submit(' + BIZ_QUERY + ')', function (data) {
        tableItemObj.reload({where: data.field})
        return false;
    });

    form.on('submit(' + BIZ_FORM + ')', function (data) {

        let url = '/save';
        let type = 'POST';
        //如果id不为空则调用修改接口
        if (data.field.id) {
            url = '/update'
        }

        $.ajax({
            url: '/' + BIZ_NAME + url,
            data: JSON.stringify(data.field),
            type: type,
            dataType: 'json',
            contentType: 'application/json',
            success: function (result) {
                if (result.code === 200) {
                    layer.msg(result.message, {icon: 1, time: 1000}, function () {
                        tableItemObj.reload();
                        layer.close(formItemObj);
                    });
                } else {
                    layer.alert(result.message, {icon: 2});
                }
            }
        })
        return false;
    });

});